package com.soushin.mvpdemo.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;

import com.blankj.ALog;
import com.hjq.toast.ToastUtils;
import com.wanglu.photoviewerlibrary.PhotoViewer;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportActivity;
import com.soushin.mvpdemo.Utils.ImageLoader;

/**
 * Activity基类
 *
 * @author SouShin
 * @time 2018/11/16 14:05
 */
public abstract class BaseActivity<V extends BaseView, P extends BasePresenter<V>> extends SupportActivity {
    private P presenter;
    private Unbinder unbinder;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        unbinder = ButterKnife.bind(this);
        presenter = createPresenter();
        bindView();
        ALog.e("当前", this.getClass().getSimpleName());
    }

    protected abstract P createPresenter();

    protected abstract int getLayoutId();

    //这个方法是为了让用户不要忘了绑定BaseView
    protected abstract void bindView();

    protected P getPresenter() {
        return presenter;
    }

    public AppCompatActivity getActivity() {
        return this;
    }

    public void goTo(Class clazz) {
        startActivity(new Intent(getActivity(), clazz));
    }

    public void goTo(Class clazz, Intent intent) {
        intent.setClass(getActivity(), clazz);
        startActivity(intent);
    }

    public void showToasty(String msg) {
        ToastUtils.show(msg);
    }

    public void showProgress(String msg) {}

    public void disProgress() {}

    /**
     * 查看大图
     *
     * @param list        数据源
     * @param rvContainer 容器 可以设置RecyclerView AbsListView
     * @param defPosition 默认位置
     *                    可以给imageView添加长按保存功能
     */
    protected void showImageList(ArrayList<String> list, RecyclerView rvContainer, int defPosition) {
        PhotoViewer.INSTANCE
                .setData(list)
                .setImgContainer(rvContainer)
                .setCurrentPage(defPosition)
                .setShowImageViewInterface(new ImageLoader())
                .start(getActivity());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        if (presenter != null) {
            presenter.unsubscribe();
        }
    }
}
